﻿Imports System.Xml

Public Class frmNewMeetingRoom
    Const Max = 20
    Dim j As Integer = 0
    Dim textbox(Max) As TextBox
    Dim arrNode As New ArrayList
    Dim _xmlFunction As New XmlFunction()

    Private Sub LoadNodeXml()
        Dim xmldoc As New XmlDocument

        Dim Doc As New XmlDocument
        Dim file_name As String
        file_name = Application.StartupPath
        If file_name.EndsWith("\bin\Debug") Then
            file_name = file_name.Substring(0, file_name.LastIndexOf("\bin\Debug"))
        End If
        file_name &= "\Xml\XmlConfigSubjects.xml"

        xmldoc.Load(file_name)
        Dim node As XmlNode = xmldoc.SelectSingleNode("/RootSubject/Subject[@type = 'Meeting']")
        Dim i As Integer = 10
        For Each n As XmlNode In node.ChildNodes
            Dim lble As Label = New Label
            arrNode.Add(n.Name)
            textbox(j) = New TextBox
            With lble
                .ForeColor = Color.Blue

                .Location = New Point(1, i)
                .Text = n.Name
                .TextAlign = ContentAlignment.BottomRight
                .Show()
            End With
            With textbox(j)
                .Name = "txt"
                .Location = New Point(110, i + 5)
                .Size = New Size(300, 25)
                .Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top

                .Show()
            End With
            Me.Controls.Add(lble)
            Me.Controls.Add(textbox(j))
            i = i + 27
            j = j + 1
        Next
        'btnClose.Location = New Point(240, i + 5)
        'btnSave.Location = New Point(110, i + 5)
        'gVStory.Location = New Point(10, i + 10 + btnAdd.Height)
        'gVStory.Height = 250
        'Me.Height = i + 50
        'gVStory.Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom
         btnAdd.Location = New Point(200, i + 5)
        btnCancle.Location = New Point(300, i + 5)
        dtgMeetingRoom.Location = New Point(10, i + 10 + btnAdd.Height)
        dtgMeetingRoom.Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top
        dtgMeetingRoom.Height = 200
        btnSave.Location = New Point(400, i + 20 + btnAdd.Height + dtgMeetingRoom.Height)
        Me.Height = i + 90 + btnAdd.Height + dtgMeetingRoom.Height
    End Sub

    Private Sub frmNewMeetingRoom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadNodeXml()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        
        

    End Sub
    Public Sub loadDataGird()
        Dim xmldoc As New XmlDocument

        Dim Doc As New XmlDocument
        Dim file_name As String
        file_name = Application.StartupPath
        If file_name.EndsWith("\bin\Debug") Then
            file_name = file_name.Substring(0, file_name.LastIndexOf("\bin\Debug"))
        End If
        file_name &= "\Xml\XmlMeeting.xml"
        Dim ds As New DataSet()
        ds.ReadXml(file_name)
        dtgMeetingRoom.DataSource = ds.Tables(0)
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim i As Integer
        Dim k As Integer = 0
        For i = 0 To j - 1
            If textbox(i).Text = "" Then
                k = k + 1
            End If
        Next
        If k = j Then
            MessageBox.Show("Please enter data")
        Else
            Dim Doc As New XmlDocument
            Dim file_name As String
            file_name = Application.StartupPath
            If file_name.EndsWith("\bin\Debug") Then
                file_name = file_name.Substring(0, file_name.LastIndexOf("\bin\Debug"))
            End If
            file_name &= "\Xml\XmlMeeting.xml"
            ' Tạo và thêm các phần tử con cho nút này.
            Dim Node(Max) As XmlNode

            If btnAdd.Text = "Add" Then
                Doc.Load(file_name)
                ' Tạo một phần tử mới.
                Dim ItemNode As XmlNode
                ItemNode = Doc.CreateElement("Meeting")
                ' Thêm đặc tính.
                Dim Attribute As XmlAttribute
                Attribute = Doc.CreateAttribute("id")
                Attribute.Value = _xmlFunction.GetRecord(file_name) + 1
                ItemNode.Attributes.Append(Attribute)
               
                For i = 0 To j - 1
                    Node(i) = Doc.CreateElement(arrNode(i).ToString)
                    ItemNode.AppendChild(Node(i))
                    Node(i).AppendChild(Doc.CreateTextNode(Trim(textbox(i).Text)))
                    'MsgBox(textbox(i).Text)
                Next
                ' Thêm phần tử mới. Trong trường hợp này, chúng ta
                ' cho nó làm con ở cuối danh sách item.
                Doc.DocumentElement.AppendChild(ItemNode)
                ' Lưu tài liệu.
                Doc.Save(file_name)
                loadDataGird()

            ElseIf btnAdd.Text = "Update" Then
                Dim nodeData As XmlNode = Doc.SelectSingleNode("RootSubject/Meeting[@ id= '" & dtgMeetingRoom.CurrentRow.Cells("id").Value & "']")

                For i = 0 To j - 1
                    Dim newChild As XmlElement = Doc.CreateElement(textbox(i).Text)
                    ' nodeData.ChildNodes(i).InnerText = textbox(i).Text
                    nodeData.ChildNodes(i).ReplaceChild(newChild, nodeData.ChildNodes(1))
                Next
                Doc.Save(file_name)
                loadDataGird()
            End If
        End If
    End Sub

    Private Sub dtgMeetingRoom_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgMeetingRoom.CellClick
        If dtgMeetingRoom.CurrentRow.Cells("id").Value > 0 Then
            If e.ColumnIndex = dtgMeetingRoom.Columns("Delete").Index Then
                Dim doc As New XmlDocument
                Dim file_name As String
                file_name = Application.StartupPath
                If file_name.EndsWith("\bin\Debug") Then
                    file_name = file_name.Substring(0, file_name.LastIndexOf("\bin\Debug"))
                End If
                file_name &= "\Xml\XmlMeeting.xml"
                doc.Load(file_name)

                Dim node As XmlNode = doc.SelectSingleNode("RootSubject/Meeting[@ id= '" & dtgMeetingRoom.CurrentRow.Cells("id").Value & "']")

                If node IsNot Nothing Then
                    'di chuyển nút hiện tại tương ứng khỏi tài liệu
                    'thông qua phương thức RemoveChild(<nut can xoa>)
                    'của doc.DocumentElement
                    doc.DocumentElement.RemoveChild(node)
                End If
                doc.Save(file_name)
                loadDataGird()
            Else
                Dim i As Integer
                Dim k As Integer = 0
                For i = 0 To j - 1
                    textbox(i).Text = dtgMeetingRoom.CurrentRow.Cells(i + 1).Value
                Next
                btnAdd.Text = "Update"
                lblId.Text = dtgMeetingRoom.CurrentRow.Cells("id").Value
            End If
        End If
    End Sub

    Private Sub btnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancle.Click
        For i = 0 To j - 1
            textbox(i).Text = String.Empty
        Next
        btnAdd.Text = "Save"
        lblId.Text = "0"
    End Sub
End Class